import matplotlib.pyplot as plt
import numpy as np
import math

delta = 0.1 #采样间隔
T = 10
XK = 1
#t_0 =np.arange(0, T, 0.01)
K = list(range(1, int(T / delta) + 1))
x_measure = [0] * len(K)
#x_hat = [0] * len(K)
wk = [0] * len(K)
e1 = [0] * len(K)
e2 = [0] * len(K)
t = [0] * len(K)


for i in range(0, len(K)):
    wk[i] = np.random.normal(loc=0.0, scale=1.0, size=None)
    x_measure[i] = XK + wk[i]

for i in range(0, len(K)):
    t[i] = (K[i] - 1) * delta
x_hat =(1/len(K)) * sum(x_measure)

for i in range(0, len(K)):
    #e1[i] = x_hat[i] - XK
    e2[i] = x_measure[i] - x_hat

plt.xlabel("Time(Sec)")
plt.ylabel("xhat")
plt.plot(t,x_measure,'-o',c = 'r',label = 'measures')
plt.axhline(y=x_hat, c="k",label = 'estimates',ls="--", lw=2)
plt.legend()
plt.show()

plt.xlabel("Time(Sec)")
plt.plot(t,e2,'-o',label = 'error between estimates and measures')
plt.axhline(y=XK-x_hat, c="k",label = 'error between estimates and true',ls="--", lw=2)
plt.legend()
plt.show()

plt.xlabel("Time(Sec)")
plt.axhline(y=x_hat, c="r",label = 'estimates',ls="--", lw=2)
plt.axhline(y=XK, c="g",label = 'true',ls="--", lw=2)
plt.xlim(0,T)
plt.ylim(0.8, 1.2)
plt.legend()
plt.show()